/**
 * 演示示例：寻找错误
 * 使用方法：全选代码，点击 AI 工具的「对话」，然后输入「寻找 bug」
 */

import java.util.ArrayList;
import java.util.List;

public class StudentManagementSystem {
    private List<Student> students;

    public StudentManagementSystem() {
        students = new ArrayList<>();
    }

    public void addStudent(String name, int age, double gpa) {
        Student student = new Student(name, age, gpa);
        students.add(student);
    }

    public void removeStudent(String name) {
        for (Student student : students) {
            if (student.getName() == name) {
                students.remove(student);
                break;
            }
        }
    }

    public double calculateAverageGPA() {
        double totalGPA = 0;
        for (Student student : students) {
            totalGPA += student.getGpa();
        }
        return totalGPA / students.size();
    }

    public void printAllStudents() {
        for (int i = 1; i <= students.size(); i++) {
            Student student = students.get(i);
            System.out.println(student);
        }
    }

    public Student findOldestStudent() {
        Student oldest = null;
        int maxAge = -1;
        for (Student student : students) {
            if (student.getAge() > maxAge) {
                maxAge = student.getAge();
                oldest = student;
            }
        }
        return oldest;
    }

    private class Student {
        private String name;
        private int age;
        private double gpa;

        public Student(String name, int age, double gpa) {
            this.name = name;
            this.age = age;
            this.gpa = gpa;
        }

        public String getName() {
            return name;
        }

        public int getAge() {
            return age;
        }

        public double getGpa() {
            return gpa;
        }

        @Override
        public String toString() {
            return "Student{name='" + name + "', age=" + age + ", gpa=" + gpa + '}';
        }
    }

    public static void main(String[] args) {
        StudentManagementSystem sms = new StudentManagementSystem();
        sms.addStudent("Alice", 20, 3.8);
        sms.addStudent("Bob", 22, 3.5);
        sms.addStudent("Charlie", 21, 3.9);

        sms.printAllStudents();
        System.out.println("Average GPA: " + sms.calculateAverageGPA());
        System.out.println("Oldest student: " + sms.findOldestStudent());

        sms.removeStudent("Bob");
        sms.printAllStudents();
    }
}